package com.example.springbootswagger2.controller;

import com.example.springbootswagger2.common.RestResult;
import com.example.springbootswagger2.common.RestResultFactory;
import com.example.springbootswagger2.model.Student;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(value = "Swagger2RestController", description = "学生服务")
@RestController
public class Swagger2RestController {

    /**
     * 组合所有的学生信息
     */
    List<Student> students = new ArrayList<Student>();

    {
        students.add(new Student("tony", "3", "China"));
        students.add(new Student("john", "2", "China"));
    }

    @ApiOperation(value = "以列表形式返回学生信息", responseContainer = "List", response = Student.class, tags = "getStudents",
            authorizations = {@Authorization(value = "read_token")})// TODO authorizations权限设置，但是没有发现实际有什么用
    @ApiResponses(value = {@ApiResponse(code = 200, message = "Suceess|OK"), @ApiResponse(code = 401, message = "not authorized!")})
    @RequestMapping(value = "/getStudents", method = RequestMethod.GET)
    public List<Student> getStudents() {
        return students;
    }

    @ApiOperation(value = "获取指定名字的学生", response = Student.class, tags = "getStudentByName",
            authorizations = {@Authorization(value = "read_token")})
    @RequestMapping(value = "/getStudent/{studentName}", method = RequestMethod.GET)
    public Student getStudentByName(@RequestParam @ApiParam(value = "studentName") String name) {
        return students.stream().filter(x -> x.getName().equalsIgnoreCase(name)).collect(Collectors.toList()).get(0);
    }

    @ApiOperation(value = "获取指定国家的学生", responseContainer = "List", response = Student.class, tags = "getStudentByCountry",
            authorizations = {@Authorization(value = "read_token")})
    @RequestMapping(value = "/getStudentByCountry/{country}", method = RequestMethod.GET)
    public List<Student> getStudentByCountry(@PathVariable(value = "country") String country) {
        System.out.println("Searching Student in country : " + country);
        List<Student> studentsByCountry = students.stream().filter(x -> x.getCountry().equalsIgnoreCase(country))
                .collect(Collectors.toList());
        System.out.println(studentsByCountry);
        return studentsByCountry;
    }

    @ApiOperation(value = "获取指定班级的学生", responseContainer = "List", response = Student.class, tags = "getStudentByClass",
            authorizations = {@Authorization(value = "read_token")})
    @RequestMapping(value = "/getStudentByClass/{cls}", method = RequestMethod.GET)
    public List<Student> getStudentByClass(@PathVariable(value = "cls") String cls) {
        return students.stream().filter(x -> x.getCls().equalsIgnoreCase(cls)).collect(Collectors.toList());
    }

    @ApiOperation(value = "添加学生", tags = "addStudent", authorizations = {@Authorization(value = "write_token")})
    @RequestMapping(value = "/addStudent", method = RequestMethod.POST, consumes = {"application/json"}, produces = {"application/json"})
    public Boolean addStudent(@ApiParam(value = "student") @RequestBody Student student) {
        return students.add(student);
    }

    @ApiOperation(value = "添加学生V2", tags = "addStudentV2")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "cls", value = "班级", paramType = "query"),
            @ApiImplicitParam(name = "country", value = "国家", paramType = "query")
    })
    @RequestMapping(value = "/addStudentV2", method = RequestMethod.GET)
    public Boolean addStudentV2(@RequestParam String name, @RequestParam String cls, @RequestParam String country) {
        Student student = new Student(name, cls, country);
        return students.add(student);
    }

    @ApiOperation(value = "查找指定班级指定名字的学生", tags = "getStudentByNameAndCls")
    @RequestMapping(value = "getStudentByNameAndCls", method = RequestMethod.GET)
    public Student getStudentByNameAndCls(@RequestParam String name, @RequestParam String cls) {
        return students.stream().filter(x -> x.getCls().equals(cls) && x.getName().equalsIgnoreCase(name))
                .collect(Collectors.toList()).get(0);
    }

    @ApiOperation(value = "获取mock数据", tags = "getStudents")
    @RequestMapping(value = "/getMockData", method = RequestMethod.GET)
    public RestResult getMockData() {

        return RestResultFactory.createSuccessResult();
    }

}
